<template>
  <div>
     <pre>
           //{{ underlineToCamel($props.tableName) }}.entity.ts
     import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
        @Entity('{{ $props.tableName }}')
        export class  {{ underlineToCamel($props.tableName) }}Entity{
             <template v-for="(colitem,index) in ($props.columns||[])">
                <span v-if="colitem.colkey=='id'"> @PrimaryGeneratedColumn()</span>
                <span v-else> @Column({name:'{{ colitem.colkey }}'})</span>
                 {{ underlineToCamel(colitem.colkey || '') }}:{{ type2tstype(colitem.coltype || '') }}
             </template>


               constructor(_data: any) {
                 <template v-for="(colitem,index) in ($props.columns||[])">
                      this.{{ underlineToCamel(colitem.colkey) }} = _data?.{{ underlineToCamel(colitem.colkey) }};
                 </template>
              }

        }
      </pre>
  </div>
</template>
<script>

function type2tstype(texttype) {
  if (texttype == 'integer') {
    return 'number';
  } else if (texttype == 'text') {
    return 'string';
  }
  return 'string'
}

function underlineToCamel(str) {
  return str?.replace(/_([a-z])/g, function (match, group1) {
    return group1.toUpperCase();
  });
}

export default {
  data() {
    return {
      type2tstype,
      underlineToCamel,
    };
  },

  props: {
    columns: { type: [Array] },
    tableName: { type: [String] },
  },
};
</script>
